/**
 * Copyright 2012 Ricardo Engel
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mookt.dcubed;

import java.io.Serializable;
import java.util.Objects;

/**
 * This class consists of {@code static} utility methods for operating on
 * {@link Entity} objects.
 * 
 * @author Ricardo Engel
 */
public final class Entities {

    private Entities() {
        throw new AssertionError("No " + Entities.class.getName()
                + " instances for you!");
    }

    /**
     * Generates a hash code for an entity.
     * 
     * <p>
     * This method is useful for implementing the hash code as specified in the
     * {@link Entity#hashCode()} contract. Thus, for any entity we can implement
     * the following: <blockquote>
     * 
     * <pre>
     * &#064;Override
     * public int hashCode() {
     *     return Entities.hashCode(this);
     * }
     * </pre>
     * 
     * </blockquote>
     * 
     * <b>Warning: The returned value does not equal the hash code of that
     * entitity's identification object hash code.</b> This value can be
     * computed by calling {@code Entity#getId().hashCode(Object)}.</b>
     * 
     * @param entity the entity to be hashed
     * @return a hash value of the input entity
     * @see Entity#hashCode()
     */
    public static <T extends Entity<ID>, ID extends Serializable> int hashCode(
            T entity) {
        return Objects.hash(entity.getId());
    }

    /**
     * Returns {@code true} if the arguments are equal to each other and
     * {@code false} otherwise.
     * 
     * <p>
     * This method is useful for implementing the equality as specified in the
     * {@link Entity#equals(Object)} contract. Thus, for any entity we can
     * implement the following: <blockquote>
     * 
     * <pre>
     * &#064;Override
     * public boolean equals(Object obj) {
     *     return Entities.equals(this, obj);
     * }
     * </pre>
     * 
     * </blockquote>
     * 
     * @param e an entity
     * @param o an object to be compared with {@code a} for equality
     * @return {@code true} if the arguments are equal to each other and
     *         {@code false} otherwise
     * @see Entity#equals(Object)
     */
    public static <T extends Entity<ID>, ID extends Serializable> boolean equals(
            T e, Object o) {
        if (o == e) {
            return true;
        } else if (e == null || o == null) {
            return false;
        } else if (o.getClass().isAssignableFrom(e.getClass())) {
            @SuppressWarnings("unchecked")
            T other = (T) o;
            return e.getId().equals(other.getId());
        } else {
            return false;
        }
    }
}
